/**
 * Created by wdj on 16/4/22.
 * MessageController
 */
'use strict';
var Controller = require('../Controller');
var $ = Dom7;

class MessageController extends Controller {


    onCreated(app, page) {

        // console.log(page.query);

        // 会话flag
        var conversationStarted = false;
        // Init Messages
        var myMessages = app.messages('.messages', {
            autoLayout:true
        });


        // Init Messagebar
        var myMessagebar = app.messagebar('.messagebar');

        $('.messagebar .link').on('click', function () {
            // Message text
            var messageText = myMessagebar.value().trim();
            // Exit if empy message
            if (messageText.length === 0) return;

            // Empty messagebar
            myMessagebar.clear()

            // 随机消息类型
            var messageType = (['sent', 'received'])[Math.round(Math.random())];

            // 接收的消息的头像和名称
            var avatar, name;
            if(messageType === 'received') {
                avatar = 'http://statics.maosui.com/Res/ResReadOlnyServer.ashx?key=/Common/20160411/fc591825d79d45a496e74faafb11f11f.png&Width=80&Height=80';
                name = '凯特';
            }
            // Add message
            myMessages.addMessage({
                // Message text
                text: messageText,
                // 随机消息类型
                type: messageType,
                // 头像和名称
                avatar: avatar,
                name: name,
                // 日期
                day: !conversationStarted ? '今天' : false,
                time: !conversationStarted ? (new Date()).getHours() + ':' + (new Date()).getMinutes() : false
            })

            // 更新会话flag
            conversationStarted = true;
        })
    }
}

export default MessageController;
